package com.hy.ruiji.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.ruiji.common.customException;
import com.hy.ruiji.dto.DishDto;
import com.hy.ruiji.enyity.Dish;
import com.hy.ruiji.enyity.DishFlavor;
import com.hy.ruiji.enyity.Setmeal;
import com.hy.ruiji.enyity.SetmealDish;
import com.hy.ruiji.mapper.DishMapper;
import com.hy.ruiji.service.DishFlavorService;
import com.hy.ruiji.service.DishService;
import com.hy.ruiji.service.SetmealDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * 新增菜品，同时添加相关的口味
     * @param dishDto 添加的相关数据
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表
        this.save(dishDto);
        Long dishId = dishDto.getId();//菜品的id

        List<DishFlavor> flavors = dishDto.getFlavors();//菜品的口味
        flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //保存口味的信息到菜品表
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto queryDishById(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);
        //拷贝
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //查询口味信息
        //构造条件信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(wrapper);
        dishDto.setFlavors(list);
        return dishDto;
    }

    /**
     * 根据id修改菜品和口味信息
     * @param dishDto 需要修改的信息
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //保存基本信息到菜品表
        this.updateById(dishDto);
        //清理口味表的相关数据
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(wrapper);
        //添加口味表的相关数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存口味的信息到菜品表
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 修改菜品的状态
     * @param status 传入的状态
     * @param ids 需要修改的id数组
     */
    @Override
    @Transactional
    public void updateStatus(int status, Long[] ids) {
        if(ids==null||ids.length==0){
            throw new customException("数据丢失，无法修改");
        }
        // 查询是否有菜品关联到套餐
        int count = setmealDishService.lambdaQuery()
                .in(SetmealDish::getDishId, ids)
                .count();

        if (count > 0) {
            throw new customException("菜品关联套餐，无法停售");
        }

        //条件构造器
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        //修改状态
        Dish dish = new Dish();
        dish.setStatus(status);
        this.update(dish,wrapper);
    }

    @Override
    @Transactional
    public void deleteByIds(Long[] ids) {
        if(ids==null||ids.length==0){
            throw new customException("数据丢失，无法删除");
        }
        // 查询是否有菜品关联到套餐
        int count = setmealDishService.lambdaQuery()
                .in(SetmealDish::getDishId, ids)
                .count();
        Integer count1 = this.lambdaQuery()
                .in(Dish::getId, ids)
                .eq(Dish::getStatus, 1)
                .count();
        if (count > 0) {
            throw new customException("菜品关联套餐，无法删除");
        }
        if(count1>0){
            throw new customException("菜品在售，无法删除");
        }

        //删除菜品表的基本信息
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        this.remove(wrapper);
        //删除口味表的信息
        LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(wrapper1);
    }
}
